//
//  Hear_SingleShowViewController.m
//  Hear
//
//  Created by mac on 2018/5/11.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "Hear_SingleShowViewController.h"

#import "Hear_PhonographView.h"

#import "Hear_BaseModel.h"

#import "Hear_AnswerScrollViewController.h"

#import "HRUMShareTool.h"

#import "Hear_AnswerViewController.h"

#import "HRDynamicViewController.h"

#import "HRFollowDyanmicInfoViewController.h"

@interface Hear_SingleShowViewController ()

@property (nonatomic, strong) AVPlayer *player;

@property (nonatomic, strong) AVPlayerItem *item;

@property (nonatomic, strong) Hear_PhonographView *bgview;

@property (nonatomic, strong) NSMutableArray *datasource;

@end

static BOOL isplay = YES;

@implementation Hear_SingleShowViewController

- (NSMutableArray *)datasource {
    if (!_datasource) {
        _datasource = [NSMutableArray array];
    }
    return _datasource;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
    [self requestData];
    
    [self registerNotification];
}

- (void)registerNotification {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Hear_PhonographAnswerAction:) name:@"Hear_PhonographAnswer" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Hear_PhonographPlayAction:) name:@"Hear_PhonographPlay" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Hear_PhonographRotatingImgAction:) name:@"Hear_PhonographRotatingImg" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Hear_PhonographHeaderTapAction:) name:@"Hear_PhonographHeaderTap" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Hear_PhonographCommentAction:) name:@"Hear_PhonographComment" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Hear_PhonographRotatingTapAction:) name:@"Hear_PhonographRotatingTap" object:nil];
}

- (void)setupUI {
    
    CGFloat height = ScreenHeight - kTopHeight;
    
    Hear_PhonographView *view = [[Hear_PhonographView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, height)];
    self.bgview = view;
    [self.view addSubview:view];
    
}

- (void)requestData {
    
    YTKApi *api = [YTKApi apiGetWithUrl:@"SoundInfo/Get_GetSoundSingle" para:@{@"CurrentUserLocalId":USERID, @"LocalId":self.soundID}];
    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            
            NSDictionary *dict = request.responseJSONObject;
            
            if ([dict[@"CodeStatus"] integerValue] == 20000) {
                
                NSString *jsonStr = dict[@"BodyContent"];

                NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
                NSError *err;
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                    options:NSJSONReadingMutableContainers
                                                                      error:&err];
                if(err) {
                    [self showMessage:@"暂无数据" atPosition:ShowPosition_Center];
                } else {
                    Hear_BaseModel *baseModel = [Hear_BaseModel modelWithDictionary:dic];

                    for (Hear_PhonographRecommendModel *model in baseModel.List) {
                        [self.datasource addObject:model];
                    }

                    dispatch_async(dispatch_get_main_queue(), ^{
                        // 给界面赋值
                        self.bgview.model = self.datasource[0];
                        
                        [self creatPlayerByModel:self.datasource[0]];
                        [self.bgview.rotatingImg startRotating];
                        [self.player play];
                        
                        isplay = YES;
                    });
                }
            }
            
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            
        }];
    }];
}

- (void)creatPlayerByModel:(Hear_PhonographRecommendModel *)model {
    
    self.item = [[AVPlayerItem alloc] initWithURL:GetUrlWithStr(model.SoundUrl)];
    
    self.player = [[AVPlayer alloc] initWithPlayerItem:self.item];
    
}

#pragma mark 回答话题
- (void)Hear_PhonographAnswerAction:(NSNotification *)sender {
    Hear_PhonographRecommendModel *model = sender.object;
    
    Hear_AnswerViewController *vc = [Hear_AnswerViewController new];
    
    vc.model = model;
    
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 播放话题
- (void)Hear_PhonographPlayAction:(NSNotification *)sender {
    UIButton *playbtn = sender.object;
    
    
    // 播放中,默认进入自动播放
    if (isplay) {
        [playbtn setImage:GetImgWithStr(@"Phonograph_play") forState:UIControlStateNormal];
        isplay = NO;
        [self.player pause];
    } else {
        isplay = YES;
        playbtn.hidden = YES;
        [self.player play];
    }
    
}

- (void)Hear_PhonographRotatingImgAction:(NSNotification *)sender {
    UIImageView *img = sender.object;
    if (isplay) {
        [img resumeRotate];
    } else {
        [img stopRotating];
    }
}

#pragma mark 题主头像点击
- (void)Hear_PhonographHeaderTapAction:(NSNotification *)sender {
    
    [self.player pause];
    
    Hear_PhonographRecommendModel *model = sender.object;
    
    [MobClick event:@"Hearer8"];
    HRDynamicViewController *dynamicVC = [HRDynamicViewController new];
    
    dynamicVC.currentUserID = model.MasterUserLocalId;
    [self.navigationController pushViewController:dynamicVC animated:YES];
}

#pragma mark 评论事件
- (void)Hear_PhonographCommentAction:(NSNotification *)sender {
    Hear_PhonographRecommendModel *model = sender.object;
    
    HRFollowDyanmicInfoViewController *infoVC = [HRFollowDyanmicInfoViewController new];
    infoVC.dynamicID = model.LocalId;
    [self.navigationController pushViewController:infoVC animated:YES];
}

#pragma mark 转动头像点击
- (void)Hear_PhonographRotatingTapAction:(NSNotification *)sender {
    
    [self.player pause];
    
    Hear_PhonographRecommendModel *model = sender.object;
    
    [MobClick event:@"Hearer8"];
    HRDynamicViewController *dynamicVC = [HRDynamicViewController new];
    
    dynamicVC.currentUserID = model.UserLocalId;
    [self.navigationController pushViewController:dynamicVC animated:YES];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
